/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.CustomerEntity;
import entity.PurchaseOrderEntity;
import entity.SalesOrderEntity;
import entity.SalesQuotationEntity;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author edd
 */
@Stateful
@LocalBean
public class SalesOrderSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    SalesOrderEntity salesOrder;
    PurchaseOrderEntity purchaseOrder;
    SalesQuotationEntity salesQuotation;
    CustomerEntity customer;
    
    public SalesOrderSessionBean() {
        
    }
    
    public void addSalesOrder(Long salesQuotationId, String settlementMode, double confirmedPrice) 
            throws Exception {
        
        salesQuotation = em.find(SalesQuotationEntity.class, salesQuotationId);
        if(salesQuotation == null) 
            throw new Exception("Sales Quotation does not exist.");
        else {
            //purchaseOrder = new PurchaseOrderEntity(settlementMode);
            purchaseOrder.setSalesQuotation(salesQuotation);
            
            //check and convert into customer
            customer = salesQuotation.getCustomer();
            if(customer == null) {
                convertSalesLead(salesQuotation);
            }
            
            //method to calculate confirmed lead time
            //salesOrder = new SalesOrderEntity(confirmedPrice, 100, "Processing"); //dummy values
            salesOrder.setPurchaseOrder(purchaseOrder);
            //salesOrder.setProductQuantity(purchaseOrder.getSalesQuotation().getProductQuantity());
            salesOrder.setCustomer(purchaseOrder.getSalesQuotation().getCustomer());
            //assign customer executive - the one creating this
            em.persist(salesOrder);
        }
    }
    
    public void convertSalesLead(SalesQuotationEntity salesQuotation) {
        //dunno how
    }
    
    public void editSalesOrder(Long salesOrderId, Hashtable<Long, Integer> productQuantity, 
            double confirmedPrice, int confirmedLeadTime) throws Exception {
        
        salesOrder = em.find(SalesOrderEntity.class, salesOrderId);
        if(salesOrder == null)
            throw new Exception("Sales Order does not exist.");
        else {
            //archive sales order and create a new one
            deleteSalesOrder(salesOrderId);
            //addSalesOrder(purchaseOrder.getId(), confirmedPrice);
            
        }
    }
    
    public void deleteSalesOrder(Long salesOrderId) throws Exception {
        
        salesOrder = em.find(SalesOrderEntity.class, salesOrderId);
        if(salesOrder == null)
            throw new Exception("Sales Order does not exist.");
        else if(salesOrder.isArchived())
            throw new Exception ("Sales Order has already been deleted.");
        else {
            salesOrder.setArchived(true);
            em.persist(salesOrder);
        }
    }
    
    public ArrayList<SalesOrderEntity> viewSalesOrder() {
        
        Query query = em.createQuery("SELECT s FROM SalesOrder s WHERE s.archived = :notArchived "
                + "ORDER BY s.id desc");
        query.setParameter("notArchived", "false");
        
        ArrayList listSalesOrder = new ArrayList<SalesOrderEntity>();
        
        for(Object o : query.getResultList()) {
            SalesOrderEntity s = (SalesOrderEntity) o;
            listSalesOrder.add(s);
        }
        return listSalesOrder;
    }
}
